﻿using System;
using System.Windows.Forms;
using CuteSprite.Eme.Data.Settlement;
using CuteSprite.Eme.UI.Utils;
using HmFramework.Components;
using HmFramework.Data;
using HmFramework.UI.Utils;

namespace CuteSprite.Eme.UI.Settlement
{
	public partial class FrmBillingSolutionAdmin : EmeCommonAdminForm
	{
		#region -- 属性 --

		private Int32 _SettlementSolutionID;

		/// <summary>公布价方案ID</summary>
		public Int32 SettlementSolutionID
		{
			get { return _SettlementSolutionID; }
			set { _SettlementSolutionID = value; }
		}

		private String _SettlementSolutionCode;

		/// <summary> 公布价代码 </summary>
		public String SettlementSolutionCode
		{
			get { return _SettlementSolutionCode; }
			set { _SettlementSolutionCode = value; }
		}

		#endregion -- 属性 --

		#region -- 构造 --

		/// <summary>默认构造函数</summary>
		public FrmBillingSolutionAdmin(Int32 settlementsolutionID, String settlementSolutionCode)
		{
			EntityDataSource = BsdEntityHelper.ModelSettlement;
			EntityDataTable = BsdEntityHelper.SSTableBillingSolution;

			SettlementSolutionID = settlementsolutionID;
			SettlementSolutionCode = settlementSolutionCode;

			InitForm();
		}


		#endregion -- 构造 --

		#region -- 控件初始化 --


		protected override void InitComponent()
		{
			base.InitComponent();
		}

		#endregion

		#region -- 重载自定义查询 --

		/// <summary>重载自定义查询</summary>
		/// <returns></returns>
		protected override WhereExpression MakeCurrentWhereExp()
		{
			if (SettlementSolutionID > 0)
			{
				return CurrentEntityFactory.MakeEqual(BillingSolution._.SettlementSolutionID, SettlementSolutionID);
			}
			else
			{
				return base.MakeCurrentWhereExp();
			}
		}

		#endregion

    #region -- 新增计费方案 --

    /// <summary>新增计费方案</summary>
    protected override void OnAddButtonClick()
    {
      // 创建新的实体对象
      IEntity entity = CurrentEntityOperate.Create(true);
      // 排序字段赋值，默认取分页控件的总记录数+1
      entity[EntityHelper.FieldSort] = PageBar.RecordCount + 1;

      var frm = new FrmBillingSolution(SettlementSolutionID, SettlementSolutionCode, entity);
      var result = frm.ShowDialog(this);
      if (result == DialogResult.OK || result == DialogResult.Yes) { AddEntitySuccess(frm.CurrentEntity); }

      frm.Close();
    }

    #endregion

    #region -- 编辑计费方案 --

    /// <summary>编辑计费方案</summary>
    protected override void OnEditButtonClick()
    {
      if (FindSelectRowCount() < 1)
      {
        DialogHelper.ShowWarning(null, "请选择需要编辑的{0}！".FormatWith(CurrentModelTable.DisplayName));
      }
      else
      {
        var loadEntity = ServerManagement.LoadEntityByKey(CurrentEntityOperate, CurrentModel.Name, CurrentModelTable.Name, SelectedEntity[EntityHelper.FieldPrimaryID]);

        // 创建窗体对象，优先根据编辑模板创建
        var form = new FrmBillingSolution(SettlementSolutionCode, loadEntity);
        // 如果没有编辑模板，根据新建模板创建
        var result = form.ShowDialog(this);
        if (result == DialogResult.OK)
        {
          SelectedEntity.CopyFrom(form.CurrentEntity);
          // 编辑成功
          EditEntitySuccess(form.CurrentEntity);
        }
        else if (result == DialogResult.Yes)
        {
          // 另存成功
          AddEntitySuccess(form.CurrentEntity);
        }
      }
    }

    #endregion

    #region -- 浏览计费方案 --

    /// <summary>点击浏览按钮触发</summary>
    protected override void OnViewButtonClick()
    {
      if (FindSelectRowCount() < 1)
      {
        DialogHelper.ShowWarning(null, "请选择需要浏览的{0}！".FormatWith(CurrentModelTable.DisplayName));
      }
      else
      {
        var selEntity = SelectedEntity;

        var loadEntity = ServerManagement.LoadEntityByKey(CurrentEntityOperate, CurrentModel.Name, CurrentModelTable.Name, selEntity[EntityHelper.FieldPrimaryID]);

        // 创建窗体对象，优先根据查看模板创建
        var form = new FrmBillingSolution(loadEntity, true);
        var result = form.ShowDialog(this);
      }
    }

		#endregion
	}
}